package com.himiotech.core.project.web.support;

import com.himiotech.core.project.model.entity.gen.GenTable;
import com.himiotech.core.project.service.IGenTableService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author wuwenbin
 */
@Controller
@RequestMapping("/tool/gen2")
public class Gen2Controller extends HimioBaseController {

    @Autowired
    private IGenTableService genTableService;

    @RequiresPermissions("tool:gen2:view")
    @GetMapping
    public String gen2(Model model) {
        model.addAttribute("tables", genTableService.list());
        return "tool/gen2";
    }

    @PostMapping("/genMapper")
    @RequiresPermissions("tool:gen2:genCode")
    @ResponseBody
    public HimioResult genMapper(Long tableId) {
        if (tableId == null) {
            return writeJsonErr("tableId 不能为空");
        }
        GenTable genTable = genTableService.selectGenTableById(tableId);
        Map<String,String> resultMap = genTableService.genCodeWithDataScope(genTable);
        return writeJsonOkData(resultMap);
    }
}
